/*
 * v1.2 by tonydon site:txdnet.cn
 * netbeansIDE 7.1
 */
package cn.txdnet.txweb.utils;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import org.apache.commons.lang.StringUtils;
import txdnet.util.DataUtil;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public final class StringUtil {
    
    private static final String NUMBER_FORMAT = "#.00";
    private static final String CURRENCY_FORMAT = "#,###.00";

    public static boolean isEmpty(String s) {
        return DataUtil.isEmpty(s);
    }

    public static String[] split(String ids, char c) {
        return StringUtils.split(ids, c);
    }
    
    public static String[] split(String ids, String c) {
        return StringUtils.split(ids, c);
    }

    public static String join(String[] ida, char c) {
        return StringUtils.join(ida, c);
    }
    
    public static String join(String[] ida, String c){
        return StringUtils.join(ida, c);
    }
    
    public static String join(Collection col, String c){
        return StringUtils.join(col, c);
    }

    /**
     * 将字符串数值转为长整形数组
     *
     * @param arr
     * @return
     */
    public static long[] parseArrStrToLong(String[] arr) {
        long[] iar = new long[arr.length];
        for (int k = 0; k < arr.length; k++) {
            iar[k] = Long.parseLong(arr[k]);
        }
        return iar;
    }
    
    public static long[] parseStrToLongArray(String str, String splitChar){
        String[] strArray = StringUtils.split(str, splitChar);
         long[] iar = new long[strArray.length];
        for (int k = 0; k < strArray.length; k++) {
            iar[k] = Long.parseLong(strArray[k]);
        }
        return iar;
    }


    /**
     * 将字符串数值转为整形数组
     * @param arr
     * @return 
     */
    public static int[] parseArrStrToInt(String[] arr) {
        int[] iar = new int[arr.length];
        for (int k = 0; k < arr.length; k++) {
            iar[k] = Integer.parseInt(arr[k]);
        }
        return iar;
    }
    
    public static int[] parseStrToIntArray(String str, String splitChar){
         String[] strArray = StringUtils.split(str, splitChar);
          int[] iar = new int[strArray.length];
        for (int k = 0; k < strArray.length; k++) {
            iar[k] = Integer.parseInt(strArray[k]);
        }
        return iar;
    }

    /**
     * 格式化数值 四舍五入保留2位小数
     * @param v
     * @return 
     */
    public static String formatNumber(Double v) {
        return DataUtil.formatDouble(v, NUMBER_FORMAT);
    }
    
    /**
     * 格式化货币 四舍五入保留2位小数
     * @param v
     * @return 
     */
    public static String formatCurrency(Double v){
        return DataUtil.formatDouble(v, CURRENCY_FORMAT);
    }
    

    /**
     * 将字符转为gbk url encode 编码
     * @param str
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static String encodeGBK(String str) throws UnsupportedEncodingException{
        if(StringUtil.isEmpty(str)){
            return StaticMark.STR_EMPTY;
        }
        return java.net.URLEncoder.encode(str, StaticMark.ENCODING_GBK);
    }
}
